package vn.com.unit.springmvc.mvc;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.com.unit.springmvc.domain.Vendor;
import vn.com.unit.springmvc.repo.VendorDao;

@Controller
@RequestMapping(value = "/vendor/view")
public class VendorViewController {
	@Autowired
	private VendorDao vendorDao;

	@RequestMapping(method = RequestMethod.POST)
	public String saveEditVendor(
			@Valid @ModelAttribute("editVendor") Vendor editVendor,
			BindingResult result, Model model) {
		if (!result.hasErrors()) {
			Vendor obj = vendorDao.findById(editVendor.getId());
			if (obj != null) {
				vendorDao.update(editVendor);
			}
			return "redirect:/vendor";
		} else {
			model.addAttribute("isEdit", true);
			return "vendor/view";
		}

	}
}
